package booksys.home;


import booksys.common.CurrentUser;
import booksys.common.Utils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class HomeService {
    public void loadCurrentUser(CurrentUser currentUser) {

        SqlSession sess = Utils.openSession();
        try{
            HomeDao dao = sess.getMapper(HomeDao.class);
            Manager manager = dao.findManagerById(currentUser.getUserId());
            sess.commit();

            currentUser.setUserName(manager.getMa_name());
            currentUser.setAvatar(manager.getMa_avatar());


        }catch(Exception e){
            sess.rollback();
            throw new RuntimeException("加载用户信息失败！",e);
        }finally {
            sess.close();
        }
    }

    public void setAvatar(CurrentUser currentUser, String filename) {

        SqlSession sess = Utils.openSession();
        try{
            HomeDao dao = sess.getMapper(HomeDao.class);
            dao.updateAvatar(currentUser.getUserId(),filename);
            sess.commit();


        }catch(Exception e){
            sess.rollback();
            throw new RuntimeException("更新头像失败！",e);
        }finally {
            sess.close();
        }
    }
}
